package ee.serge.util;

import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;

public class Tooltip extends Composite {

   private SimplePanel panel;
   private DecoratedPopupPanel popup;
   
   public Tooltip(final String text) {
      panel = new SimplePanel();
      final Label label = new Label("tooltip");
      panel.add(label);
      label.addMouseOverHandler(new MouseOverHandler() {
         
         @Override
         public void onMouseOver(MouseOverEvent event) {
            if (popup == null) { 
               popup = new DecoratedPopupPanel();
               popup.setWidget(new HTML(text));
            }
            popup.setPopupPosition(label.getAbsoluteLeft() + label.getOffsetWidth(), label.getAbsoluteTop());
            popup.show();
         }
      });
      
      label.addMouseOutHandler(new MouseOutHandler() {
         
         @Override
         public void onMouseOut(MouseOutEvent event) {
            popup.hide();
         }
      });
      
      initWidget(label);
   }
   
}
